package com.hzw.saas.web.hbox.orika;

import com.hzw.saas.api.storage.bo.ResourceFileBO;
import com.hzw.saas.web.hbox.constants.HBoxConsts;
import com.hzw.saas.web.hbox.dto.HBoxFileDTO;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;

/**
 * @author EZ09
 * @since 07/27/2021
 */
public class HBoxFileConverter extends BidirectionalConverter<ResourceFileBO, HBoxFileDTO> {

    @Override
    public HBoxFileDTO convertTo(ResourceFileBO source, Type<HBoxFileDTO> destinationType, MappingContext mappingContext) {
        HBoxFileDTO result = new HBoxFileDTO();
        result.setFileId(source.getPid())
            .setFolderId(source.getSupId())
            .setName(source.getName())
            .setSuffix(source.getSuffix())
            .setLength(source.getLength())
            .setDigest(source.getStrongChecksum())
            .setIntegrity(source.getIntegrity())
            .setStatus(source.getStatus())
            .setCreatorId(source.getCreatorId())
            .setOwnerId(source.getCreatorId())
            .setCreatorName(source.getCreatorName())
            .setOwnerName(source.getCreatorName())
            .setCreateDate(source.getCreateTime())
            .setUpdateDate(source.getUpdateTime());
        return result;
    }

    @Override
    public ResourceFileBO convertFrom(HBoxFileDTO source, Type<ResourceFileBO> destinationType, MappingContext mappingContext) {
        ResourceFileBO result = new ResourceFileBO();
        result.setPid(source.getFileId())
            .setCreatorId(source.getCreatorId())
            .setEditorId(source.getCreatorId())
            .setCreateTime(source.getCreateDate())
            .setUpdateTime(source.getUpdateDate());
        result.setSpaceId(HBoxConsts.SPACE_ID)
            .setSupId(source.getFolderId())
            .setName(source.getName())
            .setStatus(source.getStatus())
            .setCreatorName(source.getCreatorName())
            .setEditorName(source.getCreatorName());
        result.setVid(source.getFileId())
            .setSuffix(source.getSuffix())
            .setLength(source.getLength())
            .setWeakChecksum(0L)
            .setStrongChecksum(source.getDigest())
            .setIntegrity(source.getIntegrity());
        return result;
    }
}
